package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import model.CTGDM;
import model.KeHoachHocTapMauM;
import util.DAL;
import util.MyConnectUnit;
public class KeHoachHocTapMauD {

	private MyConnectUnit conn;
	
	/**
	 * @function constructor KeHoachHocTapMauD
	 * @throws Exception 
	 */
	public KeHoachHocTapMauD() throws Exception {
		super();
		this.conn = DAL.getDAL();
	}
	
	/**
	 * @function get all KeHoachHocTapMau
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<KeHoachHocTapMauM> getAlls(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<KeHoachHocTapMauM> ls = new ArrayList<KeHoachHocTapMauM>();
		ResultSet rs = this.conn.Selects(
						"id_khht_mau, ten, khht.id_ctgd, ten_day_du",
						"KHHT_MAU khht, CTGD ctgd",
						"khht.id_ctgd = ctgd.id_ctgd AND ten LIKE '%" + ten + "%'",
						"id_khht_mau ASC",
						limit);
		KeHoachHocTapMauM l = null;
		while (rs.next()) {
			l = new KeHoachHocTapMauM();
			l.setId_khht_mau(rs.getInt("id_khht_mau"));
			l.setTen(rs.getString("ten"));
			
			l.setId_ctgd(rs.getInt("khht.id_ctgd"));
			CTGDM ctgd = new CTGDM(); 
			ctgd.setTen_day_du(rs.getString("ten_day_du"));
			l.setTen_day_du(ctgd);
			ls.add(l);
		}
		return ls;
	}
	
	/**
	 * @function get all KeHoachHocTapMau
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<KeHoachHocTapMauM> getAll(String Condition, String Limit, String OrderBy) throws Exception {
		ArrayList<KeHoachHocTapMauM> ls = new ArrayList<KeHoachHocTapMauM>();
		ResultSet rs = this.conn.Select("KHHT_MAU", Condition, Limit, OrderBy);
		KeHoachHocTapMauM l = null;
		while (rs.next()) {
			l = new KeHoachHocTapMauM();
			l.setId_khht_mau(rs.getInt("id_khht_mau"));
			l.setTen(rs.getString("ten"));
			l.setId_ctgd(rs.getInt("id_ctgd"));
			ls.add(l);
		}
		return ls;
	}
	
	/**
	 * @function get all KeHoachHocTapMau with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<KeHoachHocTapMauM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}

	/**
	 * @function get all KeHoachHocTapMau with Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<KeHoachHocTapMauM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}
	
	/**
	 * @function get all KeHoachHocTapMau no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<KeHoachHocTapMauM> getAll() throws Exception {
		return getAll(null);
	}
	
	/**
	 * @function search ten ke hoach hoc tap mau
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public ArrayList<KeHoachHocTapMauM> searchKHHT(String ten) throws Exception {
		ArrayList<KeHoachHocTapMauM> list = getAll("ten LIKE '%" + ten + "%'");
		return list;
	}
	
	/**
	 * @function get all KeHoachHocTapMau with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<KeHoachHocTapMauM> getLimit(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<KeHoachHocTapMauM> list = getAll("ten LIKE '%" + ten + "%'", limit);
		return list;
	}
	
	/**
	 * function insert KeHoachHocTapMau
	 * @param khht
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(KeHoachHocTapMauM khht) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", khht.getTen());	
		map.put("id_ctgd", khht.getId_ctgd());
		return this.conn.Insert("KHHT_MAU", map);
	}
	/**
	 * @function update KeHoachHocTapMau
	 * @param khht
	 * @return
	 * @throws Exception
	 */
	public boolean Update(KeHoachHocTapMauM khht) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", khht.getTen());	
		map.put("id_ctgd", khht.getId_ctgd());
		return this.conn.Update("KHHT_MAU", map, "id_khht_mau = " + khht.getId_khht_mau());
	}

	/**
	 * @function delete KeHoachHocTapMau
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id) throws Exception {
		return this.conn.Delete("KHHT_MAU", "id_khht_mau = " + id);
	}
	
}